package com.yet.container.registry;

import java.io.Serializable;
import java.net.InetSocketAddress;

import com.yet.annotation.RpcAutowrie;
import com.yet.annotation.RpcService;

/**
 * 注冊信息
 */
public class RegistryMeta implements Serializable {

	private static final long serialVersionUID = 1L;

	private final Address address;

	private final ServiceMeta serviceMeta;

	public RegistryMeta(Address address, ServiceMeta serviceMeta) {
		super();
		this.address = address;
		this.serviceMeta = serviceMeta;
	}

	public Address getAddress() {
		return address;
	}

	public ServiceMeta getServiceMeta() {
		return serviceMeta;
	}
	
	@Override
	public String toString() {
		return "RegistryMeta [address=" + address + ", serviceMeta=" + serviceMeta + "]";
	}

	/**
	 * 注册地址
	 */
	public static class Address implements Serializable {

		private static final long serialVersionUID = 1L;

		String host;// 主机
		int port;// 端口号

		public Address(String host, int port) {
			this.host = host;
			this.port = port;
		}

		public Address(InetSocketAddress inetSocketAddress) {
			this(inetSocketAddress.getHostName(), inetSocketAddress.getPort());
		}

		public String getHost() {
			return host;
		}

		public void setHost(String host) {
			this.host = host;
		}

		public int getPort() {
			return port;
		}

		public void setPort(int port) {
			this.port = port;
		}

		@Override
		public int hashCode() {
			final int prime = 31;
			int result = 1;
			result = prime * result + ((host == null) ? 0 : host.hashCode());
			result = prime * result + port;
			return result;
		}

		@Override
		public boolean equals(Object obj) {
			if (this == obj)
				return true;
			if (obj == null)
				return false;
			if (getClass() != obj.getClass())
				return false;
			Address other = (Address) obj;
			if (host == null) {
				if (other.host != null)
					return false;
			} else if (!host.equals(other.host))
				return false;
			if (port != other.port)
				return false;
			return true;
		}

		@Override
		public String toString() {
			return "Address [host=" + host + ", port=" + port + "]";
		}
	}

	/**
	 * 多版本服务
	 */
	public static class ServiceMeta implements Serializable {

		private static final long serialVersionUID = 1L;

		private final String name;// 服务名

		private final String version;// 版本号

		private final String registryCenterName;

		public ServiceMeta(String name, String version) {
			super();
			this.name = name;
			this.version = version;
			this.registryCenterName = "";
		}

		public ServiceMeta(String name, String version, String registryCenterName) {
			super();
			this.name = name;
			this.version = version;
			this.registryCenterName = registryCenterName;
		}

		public ServiceMeta(RpcService rpcService) {
			this(rpcService.serviceName(), rpcService.version(), rpcService.registryCenterName());
		}

		public ServiceMeta(RpcAutowrie rpcAutowrie) {
			this(rpcAutowrie.serviceName(), rpcAutowrie.version(), rpcAutowrie.registryCenterName());
		}

		public String getName() {
			return name;
		}

		public String getVersion() {
			return version;
		}
		
		public String getRegistryCenterName() {
			return registryCenterName;
		}

		@Override
		public int hashCode() {
			final int prime = 31;
			int result = 1;
			result = prime * result + ((name == null) ? 0 : name.hashCode());
			result = prime * result + ((version == null) ? 0 : version.hashCode());
			return result;
		}

		@Override
		public boolean equals(Object obj) {
			if (this == obj)
				return true;
			if (obj == null)
				return false;
			if (getClass() != obj.getClass())
				return false;
			ServiceMeta other = (ServiceMeta) obj;
			if (name == null) {
				if (other.name != null)
					return false;
			} else if (!name.equals(other.name))
				return false;
			if (version == null) {
				if (other.version != null)
					return false;
			} else if (!version.equals(other.version))
				return false;
			return true;
		}

		@Override
		public String toString() {
			return "ServiceMeta [name=" + name + ", version=" + version + "]";
		}
	}
}
